package io.renren.rabbitmq;

import io.renren.config.MQConfig;
import io.renren.service.XlsShopOrdersService;
import io.renren.service.XlsUserAwardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MQReceiver {
	@Autowired
	private XlsShopOrdersService shopOrdersService;
	@Autowired
	private XlsUserAwardService accountService;
	private static Logger log = LoggerFactory.getLogger(MQReceiver.class);

	/**
	 * 支付后续操作队列(不含上级)
	 * @param orderunique
	 */
	@RabbitListener(queues= MQConfig.OPERATEAFTERPATQUEUE)
	public void receiveManagentQueue(String orderunique) {
		log.info("OPERATEAFTERPATQUEUE message:"+orderunique);
		//支付后续操作队列
//		Integer userId=Integer.parseInt(map.get("userId"));
//		BigDecimal totalCash=new BigDecimal(map.get("totalCash"));
//		String orderNo=map.get("orderNo");
		System.out.println("-----------------------订单支付后操作--------------------------");
//		shopOrdersService.operateAfterPay(orderunique);
	}

	/**
	 * 用户升级（支付后）
	 * @param userIds 用户id
	 */
	@RabbitListener(queues= MQConfig.USERUPGRADEQUEUE)
	public void receiveUpGradeQueue(String  userIds) {
		log.info("USERUPGRADEQUEUE message:"+userIds);
		//支付后续操作队列(用户升级)
		Integer userId=Integer.parseInt(userIds);
		System.out.println("--------------------------用户升级-------------------------");
//		accountService.userUpgrade(userId);
	}

	/**
	 * 未支付订单处理
	 * @param orderunique 接收到的消息 “orderunique"订单流水号
	 */
	@RabbitListener(queues = {MQConfig.ORDER_QUEUE_NAME})
	public void receiveDelay(String orderunique) {
		log.info("ORDER_QUEUE_NAME message:" +"接收时间"+new Date()+"流水号："+orderunique);
		System.out.println("-------------------------------订单失效-----------------------------");
		//死信队列未支付订单处理
		shopOrdersService.recoverGoodsNum(orderunique);
	}

}
